import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-09-19
 * Time: 18:40
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    public void push(int val){
        if (isFull()){
            //扩容
            this.elem = Arrays.copyOf(this.elem,this.elem.length * 2);
        }
        this.elem[usedSize] = val;
        this.usedSize++;
    }

    public boolean isFull() {
        return this.usedSize == this.elem.length;
    }

    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空！");
        }
        //this.elem[this.usedSize - 1] = null;//引用类型需要置空
        int oldVal = this.elem[usedSize - 1];
        usedSize--;
        return oldVal;
    }

    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("栈为空！");
        }
        return this.elem[usedSize - 1];
    }
}
